package com.speedricemanager.other.utils;

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class TaoUtils {
    /**
     * 需要的参数  路径   cookie   开始时间  结束时间   状态(2583164861_9223370553606051807_2452930581276148_0)
     *
     * @param arr
     * @return
     */
    public static String getPosition(String... arr){
        StringBuffer params = new StringBuffer();
        // 表单参数与get形式一样
        params.append("buyerNick").append("=").append("").append("&")
                .append("dateBegin").append("=").append(arr[2]).append("&")
                .append("dateEnd").append("=").append(arr[3]).append("&")
                .append("lastStartRow").append("=").append("2583164861_9223370553606051807_2452930581276148_0").append("&")
                .append("logisticsService").append("=").append("").append("&")
                .append("options").append("=").append("0").append("&")
                .append("orderStatus").append("=").append("").append("&")
                .append("pageNum").append("=").append("1").append("&")
                .append("pageSize").append("=").append("1000").append("&")
                .append("queryBizType").append("=").append("").append("&")
                .append("queryOrder").append("=").append("arc").append("&")
                .append("rateStatus").append("=").append("").append("&")
                .append("refund").append("=").append("").append("&")
                .append("sellerNick").append("=").append("").append("&")
                .append("auctionStatus").append("=").append("SUCCESS").append("&")
                .append("prePageNo").append("=").append("1");
        return postDownloadJson(arr[0],params.toString(),arr[1]);
    }

    private static   String postDownloadJson(String path,String data,String cookie){
        URL url = null;
        try {
            CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            httpURLConnection.setRequestProperty("Host",
                    "buyertrade.taobao.com");
            httpURLConnection.setRequestProperty("X-Requested-With",
                    "XMLHttpRequest");
            httpURLConnection.setRequestProperty("Referer",
                    "https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm?spm=a21bo.2017.1997525045.2.48876c04Mygc2x");
            httpURLConnection.setRequestProperty("content-type",
                    "application/x-www-form-urlencoded; charset=UTF-8");
            httpURLConnection.setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/56.0");
            httpURLConnection.setRequestProperty("Accept",
                    "application/json, text/javascript, */*; q=0.01");
            httpURLConnection.setDoOutput(true);

            httpURLConnection.setRequestProperty("Cookie",cookie);
            httpURLConnection.setDoInput(true);
            byte[] bypes = data.getBytes();
            httpURLConnection.getOutputStream().write(bypes);
            //开始获取数据
            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len;
            byte[] arr = new byte[5048];
            while((len=bis.read(arr))!= -1){
                bos.write(arr,0,len);
                bos.flush();
            }
            bos.close();
            httpURLConnection.disconnect();
            return bos.toString("gbk");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        Log.e(null, result);
        return result;
    }

    public static String format(){
        SimpleDateFormat    formatter    =   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date    curDate    =   new Date(System.currentTimeMillis());//获取当前时间
        return formatter.format(curDate);
    }
    public static String dateToStamp(String s) {
        String res = null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            res = String.valueOf(ts);
        }catch (Exception e){

        }
        return res;
    }


}
